<?php
/**
 * Operate On Matches Tables
 * 
 */

// Rebuild Ressource Search Engine 
use Fox\Html\CheckBox;
use Fox\Dao\SqlFragment;
use Hive\Ressource;
use Fox\Html\Interval;
use Fox\Html\Block;
use Fox\Html\Screen;

$xview = Xview::instance();
set_time_limit(30000);

$screen = new Screen('bexe','execute');
$screen->pageTitle('Ressource Search Engine Synchronize');
$block = new Block('bExec','Parameters');
$block->add(new Interval('ressource_id',true));
$block->add(new CheckBox('experiences'))->label('Experiences');
$block->add(new CheckBox('wishes'))->label('Wishes');
$block->add(new CheckBox('ressources'))->label('Master Data');
$block->add(new CheckBox('interviews'))->label('Interview');
$screen->add($block);
// 

if($screen->submitted()) {
	$finder = new Ressource();
	$fields = $screen->get();
	if(!$screen->errors()) 
	{
		$commit = true;
		$sync = false;
		$fragments = $block->item('ressource_id')->fragments();
		$contacts = $finder->find(SqlFragment::combine($fragments));
		if($commit) {
			$timer = new Fox\Debug\Timer();
			$timer->startRun();
			foreach($contacts as $contact) {
				
				/* @var $contact \Hive\Ressource */
				// Add Contact to the search engine
				$searchEngine = new \Fox\SearchEngine();
				$rid = $contact->getId();
				$rClass = $contact::className();
				
				try {
					if($sync) {
						$searchEngine->synchronize($contact,$contact->getId(),$contact::className());
					} else {
						$searchEngine->add($contact,$contact->getId(),$contact::className());
					}
				}
				catch(\Exception $e) {
					Fox\Message::Error('Ressource :'.$rid);
				}
				
				// Add Experience to the search engine
				foreach($contact->experiences() as $experience) {
					try {
						if($sync) {
							$searchEngine->synchronize($experience,$rid,$rClass);
						} else {
							$searchEngine->add($experience,$rid,$rClass);
						}
						}
					catch(\Exception $e) {
						Fox\Message::Error('Ressource :'.$rid.' - Experience :'.$experience->getInlineId());
					}
				}
				// Add Desire to the search engine
				foreach($contact->wishes() as $wish) {
					try {
						if($sync) {
							$searchEngine->synchronize($wish,$rid,$rClass);
						} else {
							$searchEngine->add($wish,$rid,$rClass);
						}
					}
					catch(\Exception $e) {
						Fox\Message::Error('Ressource :'.$rid.' - Wishe :'.$wish->getInlineId());
					}
				}
				// Add Desire to the search engine
				foreach($contact->interviews() as $interview) {
					try {
						if($sync) { 
							$searchEngine->synchronize($interview,$rid,$rClass);
							
						} else {
							$searchEngine->add($interview,$rid,$rClass);
						}
					}
					catch(\Exception $e) {
						Fox\Message::Error('Ressource :'.$rid.' - Interview :'.$interview->getInlineId());
					}
				}
				Fox\Message::Success('Ressource :'.$rid.' has been updated.');
			}
			$timer->endRun();
			echo '<h3>Nb Contact '.count($contacts).'</h3>';
			var_dump($timer->intermediateDeltas());
		}
	}
}
echo $screen->display();